package com.example.personalinfoapp;

import android.os.Bundle;
import android.text.Editable;
import android.text.InputType;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {

    // 声明所有需要用到的控件
    private EditText etUsername;
    private EditText etPassword;
    private CheckBox cbAgreement;
    private Button btnLogin;
    private TextView tvRegister;
    private CheckBox cbShowPassword;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // 绑定布局中的控件
        etUsername = findViewById(R.id.et_username);
        etPassword = findViewById(R.id.et_password);
        cbAgreement = findViewById(R.id.cb_agreement);
        btnLogin = findViewById(R.id.btn_login);
        tvRegister = findViewById(R.id.tv_register);
        cbShowPassword = findViewById(R.id.cb_show_password);

        // 1. 用户名输入框交互：焦点变化（边框变亮）+ 文本去空格
        etUsername.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etUsername.setBackgroundResource(R.drawable.edittext_focused);
            } else {
                etUsername.setBackgroundResource(android.R.drawable.edit_text);
            }
        });
        etUsername.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {}
            @Override
            public void afterTextChanged(Editable s) {
                String trimText = s.toString().trim();
                if (!trimText.equals(s.toString())) {
                    etUsername.setText(trimText);
                    etUsername.setSelection(trimText.length());
                }
            }
        });

        // 2. 密码输入框交互：焦点变化（边框变亮）+ 显示/隐藏密码
        etPassword.setOnFocusChangeListener((v, hasFocus) -> {
            if (hasFocus) {
                etPassword.setBackgroundResource(R.drawable.edittext_focused);
            } else {
                etPassword.setBackgroundResource(android.R.drawable.edit_text);
            }
        });
        cbShowPassword.setOnCheckedChangeListener((buttonView, isChecked) -> {
            if (isChecked) {
                // 显示密码：普通文本类型
                etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
            } else {
                // 隐藏密码：密码类型
                etPassword.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
            }
            // 保持光标在文本末尾
            etPassword.setSelection(etPassword.getText().length());
        });

        // 3. 协议勾选框：控制登录按钮启用/禁用及样式
        cbAgreement.setOnCheckedChangeListener((buttonView, isChecked) -> {
            btnLogin.setEnabled(isChecked);
            btnLogin.setBackgroundTintList(getResources().getColorStateList(
                    isChecked ? android.R.color.holo_blue_light : android.R.color.darker_gray
            ));
        });

        // 4. 登录按钮：点击校验并提示
        btnLogin.setOnClickListener(v -> {
            String username = etUsername.getText().toString().trim();
            String password = etPassword.getText().toString().trim();

            if (username.isEmpty()) {
                Toast.makeText(MainActivity.this, "请输入账号", Toast.LENGTH_SHORT).show();
            } else if (password.isEmpty()) {
                Toast.makeText(MainActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
            } else if (!cbAgreement.isChecked()) {
                Toast.makeText(MainActivity.this, "请同意用户协议", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "登录成功", Toast.LENGTH_SHORT).show();
            }
        });

        // 5. 注册提示：点击模拟跳转
        tvRegister.setOnClickListener(v ->
                Toast.makeText(MainActivity.this, "跳转到注册页", Toast.LENGTH_SHORT).show()
        );
    }
}

